﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public class ShopItem : MonoBehaviour {

    //public int itemId;//id
    //public int coinCount;//兑换时需要的金币数
    //public int phoneFee;//兑换的话费
    
    //public UILabel phoneFeeLabel;
    public GameObject goodsBtn;//兑换按钮

    public int tempID;

    public UILabel Title;

    public GameObject texture;

    public UILabel coinCountLabel;


    public string textureUrl;
    void Awake () {
        UIEventListener.Get(goodsBtn).onClick = ButtonClick;

	}
    private void Start()
    {
        UIEventListener.Get(goodsBtn).onClick = ButtonClick;
    }
    public void ButtonClick(GameObject button)
    {
        if (button.name == "item")
        {
            Debug.Log("商店购买");

            // 加载兑换页面
            GameObject Conversion = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)Resources.Load(HallPrafabPath.Conversion));
            SwopModel swopModel = Conversion.GetComponent<SwopModel>();
            if (swopModel)
            {
                swopModel.Id = tempID;
                swopModel.Price.text = coinCountLabel.text;
                swopModel.Title = this.Title;
                StartCoroutine(GetWWWFACE(textureUrl, swopModel.item));

                string name = PlayerPrefs.GetString("consigneeName");
                string phone = PlayerPrefs.GetString("consigneePhone");
                string home = PlayerPrefs.GetString("consigneeHome");
                if (name != "" && phone != "" && home != "")
                {
                    swopModel.inputUserName.value = name;
                    swopModel.inputUserPhone.value = phone;
                    swopModel.inputUserHome.value = home;
                }
            }
        }
    }
    int width, height;
    IEnumerator GetWWWFACE(string url, GameObject go)
    {
        if (url != "")
        {
            WWW www = new WWW(url);
            yield return www;
            if (www.isDone)
            {

                if (www.error == null) { }
                //Avatar.mainTexture = www.texture;
                go.AddComponent<UITexture>().mainTexture = www.texture;

                width = www.texture.width;
                height = www.texture.height;

                go.GetComponent<UITexture>().depth = 5;

                // 0是宽，1是高
                int[] ints = Set(width, height);
                
                go.GetComponent<UITexture>().width = ints[0];
                go.GetComponent<UITexture>().height = ints[1];
            }
            else
            {

            }
            www.Dispose();
        }
    }

    /// <summary>
    /// 计算比例并根据给定的高返回宽
    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public int[] Set(int width, int height)
    {
        // 0是宽，1是高
        int[] ints = new int[2];


        float wid = width;
        float hei = height;

        float bili = wid / hei;

        bili = Mathf.Abs(bili);

        // 根据宽计算高
        //ints[0] = 80;
        //ints[1] = Mathf.CeilToInt(ints[0] / bili);

        // 根据高计算宽
        ints[1] = 120;
        ints[0] = Mathf.CeilToInt(ints[1] * bili);

        return ints;
    }

}
